<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\member\controller;
use think\exception\Jump;
use think\facade\View;
use app\member\model\Login as LoginModel;
use app\admin\model\Point as PointModel;
use app\member\validate\member_validate;
class Login{
    
    
    // 会员登录控制器
    public function login(){
        if (request()->isPost()) {
             // 获取全部参数
    	    $postField = 'username,password,code';
			$data = request()->only(explode(',',$postField),'post',null);
            if(!$data['username']) Jump::error("用户名不能为空!");
    		if(!$data['password']) Jump::error("密码不能为空!"); 
    	     // 如果开启验证码则需要验证码登录
			 if(get_config('member_chek')){
			    if(!captcha_check($data['code'])){
                  Jump::error('验证码错误!');
                }; 
			 }
		  	$password = password($data['password']);
    		$where = is_email($data['username']) ? ['email'=>$data['username']] : ['username'=>$data['username']];
    		$member = LoginModel::where($where)->find();
		  	// 判断密码是否正确
    		if(!$member || ($member['password']!=$password)) Jump::error("密码错误!"); 
    		// 判断用户状态类型
    		if($member['status'] == '0') Jump::error("用户未通过审核!");
    		else if($member['status'] == '2') Jump::error("用户已锁定!");
    		else if($member['status'] == '3') Jump::error("用户已被管理员拒绝登录!");
    		
    		session('_userid',$member['userid']);
    		session('_username',$member['username']);
    		session('_groupid',$member['groupid']);
    		session('_nickname',$member['nickname']);
    		$last_day = date('d', $member['lastdate']);
    		
    		//如果是当天第一次登录则获取积分
    		if($last_day != date('d')  &&  time()>$member['lastdate'] && get_config('login_point')>0) {
    			PointModel::point_add('1',get_config('login_point'),'0',$member['userid'],$member['username'],$member['experience']);
    		}
    		// 更新vip状态
    		if($member['vip'] && $member['overduedate']<time())	LoginModel::where('userid',$member['userid'])->update(['vip' => 0]);
    		LoginModel::where('userid',$member['userid'])->update(['lastip'=>getip(),'lastdate'=>time()]);
    		LoginModel::where('userid',$member['userid'])->setInc('loginnum');
    		Jump::success("登录成功!",'/member/index/index');
        }
        // 	检查本地是否有记录如果有则直接登录
    	if(Session('_userid')) Jump::success("登录成功!", url('/member/index/index'));
        return View('index/login');
    }
    
    
    
    
    
    // 注册账户
    public function register() {
    	if (request()->isPost()) {
    	    if(get_config('member_register') == 0) Jump::error("管理员关闭了新会员注册!");
             // 获取全部参数
    	    $postField = 'username,password,code,email';
			$data = request()->only(explode(',',$postField),'post',null);
             if(get_config('member_isregister')){
			    if(!captcha_check($data['code'])){
                  Jump::error('验证码错误!');
                }; 
			 }
    		 // 加载验证器
    	    $validate = new member_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
    		
    		$result = LoginModel::where('username',$_POST['username'])->value('userid');
    		if($result) Jump::error("该用户名已注册");
    		$result = LoginModel::where('email',$_POST['email'])->value('userid');
    		if($result) Jump::error("该邮箱已注册！");
    		
    		$arr['nickname'] = $arr['username'] = $_POST['username'];
    		$arr["password"] = password($_POST['password']);
    		$arr["email"] = $_POST['email'];
    		$arr['regdate'] = $arr['lastdate'] = time();
    		$arr['regip'] = $arr['lastip'] = getip();
    		$arr['groupid'] = '1';
    		$arr['sex'] = '1';
    		$arr['amount'] = '0.00';
    		
    		$arr['qq'] = '';
            $arr['phone'] = '';
            $arr['userpic'] = '';
            $arr['birthday'] = '';
            $arr['area'] = '';
            $arr['motto'] = '';
            $arr['cover'] = '';
            $arr['openid'] = '';
            
    		
    		$arr['point'] = $arr['experience'] = get_config('member_point');
    		$arr['status'] = (get_config('member_qh')) ? 0 : 1;
    		$a = LoginModel::insert($arr);
    		if(!$a) Jump::error("注册失败！");
    		
    		$userId = LoginModel::getLastInsID();
    		$member = LoginModel::where('userid',$userId)->find();
    		
    		session('_userid',$member['userid']);
    		session('_username',$member['username']);
    		session('_groupid',$member['groupid']);
    		session('_nickname',$member['nickname']);
    		
    		Jump::success("注册成功!",'/member/index/index');
    	}
    }    
    
    
    
    // 退出登录
    public function logout() {
    	Session('_userid',null);
    	Session('_username',null);
    	Session('_groupid',null);
    	Session('_nickname',null);
    	Jump::success("退出成功!",'/index');
    }
    
    
}